package cn.kginglam.demo.server.impl;

import cn.kginglam.demo.domain.BkUser;
import cn.kginglam.demo.mapper.BkUserMapper;
import cn.kginglam.demo.server.BkUserServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BkUserServerImpl implements BkUserServer {

    @Autowired
    private BkUserMapper userMapper;

    @Override
    public BkUser regiter(BkUser user) {

        if (judegeAccount(user)) {  // 用户名或者密码为空
            return null;
        }

        BkUser bkUsers = userMapper.selctByName(user);
        if (bkUsers != null) {
            System.out.println("账号已经存在");
            return null;
        }

        int insert = userMapper.insert(user);
        if (insert == 1) {      // 插入成功
            return user;
        }
        return null;
    }

    @Override
    public BkUser login(BkUser user) {

        if (judegeAccount(user)) { // 用户名或者密码为空

            return null;
        }

        BkUser bkUser = userMapper.selctByName(user);
        if (bkUser == null) {
            System.out.println("该账号暂未注册");
            return null;
        }
        return bkUser;
    }

    /* 判断用户密码是否为空 */
    public boolean judegeAccount(BkUser user) {
        if (StringUtils.isBlank(user.getName())) {
            System.out.println("账号不能为空");
            return true;
        }

        if (StringUtils.isBlank(user.getPassword())) {
            System.out.println("密码不能为空");
            return true;
        }
        return false;
    }
}
